#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <time.h>

#include "lm_gb28_man.h"
#include "lm_av_simulate.h"


LmAvSimulate *g_avcomm  = NULL;
LmAvSimulate *g_avcomm1 = NULL;

int live_play_cb(Frame_st *&frame)
{
	if(g_avcomm){
		return g_avcomm->GetStream(frame);
	}

	return -1;
}

int play_back_cb(void* para)
{
	Frame_st *pFrame = (Frame_st*)para;
	if(g_avcomm){
		g_avcomm->GetStream(pFrame);
	}
	
	return 0;
}

int start_device0_gb28Client(lmGb28Man *tGb28Man)
{
	deviceInfo_st devinf1 = {0};
	
	/// easygbs: ip,port,serverip,domain
	uchar tServer_id[30]    = "34020000002000000001"; //123 "13110610025571659000";:121"13101811171909469000";
	uchar tServer_ip[20]    = "192.168.197.1";
	ushort tServer_port     = 15060;

	/// local ipc:
	uchar  dev_id[30]	    = "34020000001340000001";
	//uchar  dev_pwd[20]      = "12345678";
	uchar  sip_pwd[20]      = "easygbs1";
	uchar  dev_ip[20]       = "192.168.197.176";
	ushort dev_media_port   = 20000;
	ushort dev_sess_port    = 5080;

	uchar dev_alarm_id[30]	  = "34020000001340000010"; //"001200000410010"; //"34020000001340000005";

	/// rtp: av: localip:
	uchar  dev_media_ip[30]   = "192.168.197.176";
	ushort dev_media_rtpPort  = 15070;

	char dev_name[30]         = "zhangsanceshi";
	char dev_manufacturer[30] = "dyt";
	char dev_model[30]        = "ABC_model2";
	char dev_firmware[30]     = "V1.0";

	memcpy(devinf1.server_id, tServer_id, strlen((const char*)tServer_id));
	memcpy(devinf1.server_ip, tServer_ip, strlen((const char*)tServer_ip));
	devinf1.server_port = tServer_port;
	
	memcpy(devinf1.ipc_id,  dev_id, strlen((const char*)dev_id));
	memcpy(devinf1.ipc_pwd, sip_pwd, strlen((const char*)sip_pwd));
	memcpy(devinf1.ipc_ip,  dev_ip, strlen((const char*)dev_ip));
	devinf1.ipc_media_port = dev_media_port;
	devinf1.ipc_sess_port  = dev_sess_port;
	
	memcpy(devinf1.media_ip, dev_media_ip, strlen((const char*)dev_media_ip));
	devinf1.media_port  = dev_media_rtpPort;

	memcpy(devinf1.device_name, dev_name, strlen((const char*)dev_name));
	memcpy(devinf1.device_manufacturer, dev_manufacturer, strlen((const char*)dev_manufacturer));
	memcpy(devinf1.device_model,    dev_model, strlen((const char*)dev_model));
	memcpy(devinf1.device_firmware, dev_firmware, strlen((const char*)dev_firmware));
	memcpy(devinf1.alarm_id, dev_alarm_id, strlen((const char*)dev_alarm_id));

	devinf1.serial_num  = (uint)random();

	gb_callback_st callback;
	memset(&callback, 0, sizeof(gb_callback_st));
	callback.liveCallFun     = live_play_cb;

	tGb28Man->lm_device_start(&devinf1, &callback);

	return 0;
}

int start_device1_gb28Client(lmGb28Man *tGb28Man)
{
	deviceInfo_st devinf1 = {0};
	
	// easygbs: ip,port,serverip,domain
	uchar tServer_id[30]    = "34020000002000000001"; //123 "13110610025571659000";:121"13101811171909469000";
	uchar tServer_ip[20]    = "192.168.197.1";
	ushort tServer_port     = 15060;

	/// local ipc:
	uchar  dev_id[30]	    = "34020000001310000001";
	uchar  dev_pwd[20]      = "easygbs1";
	uchar  dev_ip[20]       = "192.168.197.167";
	ushort dev_media_port   = 20000;
	ushort dev_sess_port    = 5080;

	uchar dev_alarm_id[30]	= "34020000001310000010"; //"001200000410010"; //"34020000001340000005";

	/// rtp: av: localip:
	uchar  dev_media_ip[30]  = "192.168.197.167";
	ushort dev_media_rtpPort = 15070;

	char device_name[30]         = "wangmaziceshi";
	char device_manufacturer[30] = "dyt";
	char device_model[30]        = "ABC_model1";
	char device_firmware[30]     = "V1.0";

	memcpy(devinf1.server_id, tServer_id, strlen((const char*)tServer_id));
	memcpy(devinf1.server_ip, tServer_ip, strlen((const char*)tServer_ip));
	devinf1.server_port = tServer_port;
	
	memcpy(devinf1.ipc_id,  dev_id, strlen((const char*)dev_id));
	memcpy(devinf1.ipc_pwd, dev_pwd, strlen((const char*)dev_pwd));
	memcpy(devinf1.ipc_ip,  dev_ip, strlen((const char*)dev_ip));
	devinf1.ipc_media_port = dev_media_port;
	devinf1.ipc_sess_port  = dev_sess_port;
	
	memcpy(devinf1.media_ip, dev_media_ip, strlen((const char*)dev_media_ip));
	devinf1.media_port  = dev_media_rtpPort;

	memcpy(devinf1.device_name, device_name, strlen((const char*)device_name));
	memcpy(devinf1.device_manufacturer, device_manufacturer, strlen((const char*)device_manufacturer));
	memcpy(devinf1.device_model,    device_model, strlen((const char*)device_model));
	memcpy(devinf1.device_firmware, device_firmware, strlen((const char*)device_firmware));
	memcpy(devinf1.alarm_id, dev_alarm_id, strlen((const char*)dev_alarm_id));

	devinf1.serial_num     = (uint)random();

	gb_callback_st callback0;
	memset(&callback0, 0, sizeof(gb_callback_st));

	//callback0.playbackCallFun = play_back_cb;
	callback0.liveCallFun	 = live_play_cb;

	tGb28Man->lm_device_start(&devinf1, &callback0);

	return 0;
}

//int test1_main()
int main()
{
	lmGb28Man gb28Man;

#if 1
	//test1 device 1.....
	g_avcomm = new LmAvSimulate;
	file_st file1;
	memset(&file1, 0, sizeof(file_st));
	file1.ftpy         = T_H264;
	const char *fileName = "./es/avatar.h264";
	memcpy(file1.fileNamePath, fileName, strlen(fileName));

	file_st *filet1[1];
	filet1[0]= &file1;
	g_avcomm->av_comm_init(1, filet1);
	start_device0_gb28Client(&gb28Man);
#endif

#if 0
	//test2 device 2.....
	g_avcomm1 = new LmAvSimulate;
	file_st file2[2];
	memset(&file2, 0, sizeof(file2));
	file2[0].ftpy         = T_H264;
	const char *fileName1 = "./es/avatar.h264";
	memcpy(file2[0].fileNamePath, fileName1, strlen(fileName1));
	
	file2[1].ftpy         = T_G711A;
	file2[1].frameLen     = 320;   
	const char *fileName2 = "./es/8k_1_16.g711a";
	memcpy(file2[1].fileNamePath, fileName2, strlen(fileName2));

	file_st *filet2[2];
	filet2[0] = &file2[0];
	filet2[1] = &file2[1];
	g_avcomm1->av_comm_init(2, filet2);
	start_device1_gb28Client(&gb28Man);
#endif

	gb28Man.lm_start_gb28_man();

	while(1)
	{
		sleep(1);
	}
	
	return 0;
}

